{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Known error: This notebook requires [yt](https://yt-project.org/) to visualize the results. Yt needs to be updated to work properly first. Updates are currently being made to yt's frontends to make this PyNE integration work."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from os import path, getcwd, remove\n",
    "from numpy import linspace, bitwise_or\n",
    "\n",
    "from pyne.mesh import Mesh, NativeMeshTag\n",
    "from pyne.dagmc import load, discretize_geom\n",
    "\n",
    "from yt.config import ytcfg; ytcfg[\"yt\",\"suppressStreamLogging\"] = \"True\"\n",
    "from yt.frontends.moab.api import PyneMoabHex8Dataset\n",
    "from yt.visualization.plot_window import SlicePlot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "faceted_file = path.join(getcwd(), 'teapot.h5m')\n",
    "if not path.isfile(faceted_file):\n",
    "    url = \"http://data.pyne.io/teapot.h5m\"\n",
    "    r = requests.get(url)\n",
    "    with open(\"teapot.h5m\", \"wb\") as outfile:\n",
    "        outfile.write(r.content)\n",
    "load(faceted_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_divisions = 50\n",
    "num_rays = 3\n",
    "\n",
    "coords0 = linspace(-6, 6, num_divisions)\n",
    "coords1 = linspace(0, 7, num_divisions)\n",
    "coords2 = linspace(-4, 4, num_divisions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = Mesh(structured=True, structured_coords=[coords0, coords1, coords2], structured_ordering='zyx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = discretize_geom(m, num_rays=num_rays, grid=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.vols = NativeMeshTag(1, float)\n",
    "mask = bitwise_or(results['cell'] == 1, results['cell'] == 2)\n",
    "m.vols[results['idx'][mask]] = results[mask]['vol_frac']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pf = PyneMoabHex8Dataset(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = SlicePlot(pf, 'z', 'vols')\n",
    "s.display()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
